package net.sql.config;

import java.util.List;
import java.util.Vector;

/**
 * 
 * @author 李岩飞
 * @email eliyanfei@126.com
 * @date 2018年7月25日下午1:03:46
 */
public class CacheSqlPool {
	public List<CacheSqlBean> data = new Vector<CacheSqlBean>();
	public int total = 20;

	public CacheSqlPool(int cache) {
		this.total = cache;
	}

	public Object getCacheData(String hash) {
		for (CacheSqlBean cacheSqlBean : data) {
			if (cacheSqlBean.getHash().equals(hash)) {
				cacheSqlBean.setCount(cacheSqlBean.getCount() + 1);
				return cacheSqlBean.getData();
			}
		}
		return null;
	}

	public synchronized void setCacheData(String hash, Object list) {
		CacheSqlBean cacheSqlBean = new CacheSqlBean();
		cacheSqlBean.setHash(hash);
		cacheSqlBean.setData(list);
		cacheSqlBean.setCount(1);
		CacheSqlBean minBean = null, tmpBean = null;
		if (data.size() >= total) {
			int max = Integer.MAX_VALUE;
			for (int i = 0; i < data.size() - 1; i++) {
				tmpBean = data.get(i);
				if (tmpBean.getCount() < max) {
					max = tmpBean.getCount();
					minBean = tmpBean;
				}
			}
		}
		data.remove(minBean);
		data.add(cacheSqlBean);
	}

}
